#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#include<map>
class Solution {
public:
    vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
        vector<double> result;
        map<string,pair<double,string>> M;
        for(int i = 0;i<equations.size();++i){
            string a = equations[i][0];
            string b = equations[i][1];
            double c = values[i];
            if(M[a].first==0&&M[b].first==0){
                M[a] = make_pair(c*1,b); 
                M[b] = make_pair(1,b);
             }
            else if(M[a].first!=0&&M[b].first==0){ 
                M[b] = make_pair((M[a].first)/c,a);
            }
            else if(M[a].first==0&&M[b].first!=0){
                M[a] = make_pair((M[b].first)*c,b);
            }
            else{
                while(M[a].second!=a){
                    string temp = M[a].second;
                    M[a] = make_pair(c*M[b].first,b);
                    
                    
                }              
            }
        }
        
        for(int i = 0;i<queries.size();++i){
            string a = queries[i][0];
            string b = queries[i][1];
            
            if(M.find(a)==M.end()||M.find(b)==M.end()){
                result.push_back(-1);
            }
             else{
                result.push_back((M[a])/(M[b]));
            }
        }
            
        return result;
    }
};
int main(){
    Solution S;
    vector<vector<string>> equations={{"a","b"},{"e","f"},{"b","e"}};
    vector<double> values = {3.4,1.4,2.3};
    vector<vector<string>> queries = {{"b","a"},{"a","f"},{"f","f"},{"e","e"}};
    vector<double> result = S.calcEquation(equations,values,queries);
    for(double d:result){
        cout<<d<<endl;
    }

}